<html>
<head><meta charset="utf-8"><title>Custom target + static linking · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Custom.20target.20.2B.20static.20linking.html">Custom target + static linking</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="214138171"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Custom%20target%20%2B%20static%20linking/near/214138171" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Custom.20target.20.2B.20static.20linking.html#214138171">(Oct 22 2020 at 02:38)</a>:</h4>
<p>Heya everyone! I'm building for riscv64ima-unknown-linux-gnu and thus I'm using build-std to do some fancy magic. I'm trying to get rust to compile a static binary and it really seems to want to refuse. The linking command it emits is _so close_ but it's including a "-Wl,-Bdynamic" and a "-lgcc_s" which if were omit, the static binary would successfully build. I peeked around in Rust and it seems like these are not configurable things?<br>
Here's kinda all the different commands/files: <a href="https://gist.github.com/gamozolabs/5749465e349be7289bf3f2c61ac52aa4">https://gist.github.com/gamozolabs/5749465e349be7289bf3f2c61ac52aa4</a><br>
What's being built is just hello world. It seems that internally Rust seems to key off of the environment in some <code>cfg()</code> flags (checking musl vs glibc) and glibc seems hard to coerce into building static?</p>



<a name="214138784"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Custom%20target%20%2B%20static%20linking/near/214138784" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Custom.20target.20.2B.20static.20linking.html#214138784">(Oct 22 2020 at 02:52)</a>:</h4>
<p>TL;DR: <a href="/user_uploads/4715/aYjIumUmYCofLyJTcCNzlE-q/image.png">image.png</a> If I remove these 2 flags in red I'll get a statically compiled binary that matches my requirements, but they seem to be baked into the linker command generation.</p>
<div class="message_inline_image"><a href="/user_uploads/4715/aYjIumUmYCofLyJTcCNzlE-q/image.png" title="image.png"><img src="/user_uploads/4715/aYjIumUmYCofLyJTcCNzlE-q/image.png"></a></div>



<a name="214139721"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Custom%20target%20%2B%20static%20linking/near/214139721" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Custom.20target.20.2B.20static.20linking.html#214139721">(Oct 22 2020 at 03:14)</a>:</h4>
<p>Might be a bit of a hack, but setting <code>RUSTFLAGS="-Zlink-native-libraries=off"</code> and using:</p>
<div class="codehilite"><pre><span></span><code>&quot;late-link-args&quot;: {
      &quot;gcc&quot;: [
          &quot;-Wl,-Bstatic&quot;,
          &quot;-lpthread&quot;
      ]
  },
</code></pre></div>


<p>Does the trick to get hello world building on this target staticially</p>



<a name="214146902"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Custom%20target%20%2B%20static%20linking/near/214146902" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Custom.20target.20.2B.20static.20linking.html#214146902">(Oct 22 2020 at 06:10)</a>:</h4>
<p>Have you tried passing <code> -C target-feature=+crt-static</code>?</p>



<a name="214146933"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Custom%20target%20%2B%20static%20linking/near/214146933" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Custom.20target.20.2B.20static.20linking.html#214146933">(Oct 22 2020 at 06:11)</a>:</h4>
<p>That should cause rust to stop passing <code>-Bdynamic</code>, and switch from gcc_s to the static gcc libraries.</p>



<a name="214146994"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Custom%20target%20%2B%20static%20linking/near/214146994" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Custom.20target.20.2B.20static.20linking.html#214146994">(Oct 22 2020 at 06:12)</a>:</h4>
<p>(Make sure you're using latest nightly for that. I just fixed it recently. :) )</p>



<a name="214151185"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Custom%20target%20%2B%20static%20linking/near/214151185" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Custom.20target.20.2B.20static.20linking.html#214151185">(Oct 22 2020 at 07:18)</a>:</h4>
<p>Oooh, I tried it but it "didn't work". But I'm on 1.47.0-nightly (nightly features on stable branch), I will give it a look!</p>



<a name="214151247"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Custom%20target%20%2B%20static%20linking/near/214151247" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Custom.20target.20.2B.20static.20linking.html#214151247">(Oct 22 2020 at 07:19)</a>:</h4>
<p>If it's not much digging, what's the PR/issue that tracked the fix? I'd be curious to peek at the diff and version</p>



<a name="214157588"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Custom%20target%20%2B%20static%20linking/near/214157588" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Custom.20target.20.2B.20static.20linking.html#214157588">(Oct 22 2020 at 08:33)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/pull/77386">https://github.com/rust-lang/rust/pull/77386</a> (but there was also some work required on the libc crate IIRC)</p>



<a name="214166873"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Custom%20target%20%2B%20static%20linking/near/214166873" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Custom.20target.20.2B.20static.20linking.html#214166873">(Oct 22 2020 at 10:15)</a>:</h4>
<p>Oh wow, what are the odds! Thanks! Custom target specifications + build-std are just so powerful, I'm gonna experiment a bit more with this on some other targets and architectures I work with, most of which are using static binaries</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>